/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean.custom;

import com.jb.web.application.Command;
import com.jb.web.bean.Form;
import com.jb.web.bean.UIComponentImpl;
import com.jb.web.bean.event.UIEvent;
import com.jb.web.context.WebContext;
import com.jb.web.session.SessionUser;

import com.jb.faces.component.ActionSource;


/**
 * Event handler handles most events related to a filedirectory
 */
public class FileDirectoryEH {
    public void onEvent(WebContext pCtx, UIEvent event) {
        UIComponentImpl comp = (UIComponentImpl)event.getSource();
        FileDirectory fd = 
            (FileDirectory)comp.getPage().getDescendantByClientId("fileListTree");

        if (comp instanceof Form) { //create directory

            String fileName = event.getParameter("fileName");
            FileNode file = fd.getFile();

            if (FileNode.ICONS.equalsIgnoreCase(fileName) || 
                FileNode.SMALL.equalsIgnoreCase(fileName)) {
                pCtx.addErrorMessage(this, "Directory name is reserved, please use another one - " + fileName);
                return;
            }

            if ((fileName != null) && (file != null) && 
                (file.getFile() != null)) {
                fd.getFile().createSubDir(fileName);
                fd.changeModel();
            }
        } else if (comp instanceof ActionSource) {
            if ((fd == null) || (fd.getFile() == null) || 
                (fd.getFile().getFile() == null)) {
                return;
            }

            String command = ((Command)comp).getCommand();

            if ("delete".equals(command)) {
                try {
                    int seq = Integer.parseInt(event.getParameter("seq"));
                    FileNode file = (FileNode)fd.getFileList().get(seq);

                    file.delete(pCtx);
                    fd.getFileList().remove(seq);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if ("shareDirectory".equals(command)) {
                fd.setValue("${resource.shared}");
                fd.setReadOnly(true);
                fd.setFileLocation("resources");
                fd.changeModel();
            }
            if ("docDirectory".equals(command)) {
                fd.setValue("${resource.shared}");
                fd.setReadOnly(true);
                fd.setFileLocation("doc");
                fd.changeModel();
            }

            if ("strangerDirectory".equals(command)) {
                fd.setValue("${resource.mydocument}");

                String userName = event.getParameter("userName");
                SessionUser user = 
                    pCtx.getSession().getSecurityManager().findUser(pCtx, userName);
                fd.setOwner(user);
                fd.setReadOnly(true);
                fd.setFileLocation("users/" + user);
                fd.changeModel();
            }

            if ("privateDirectory".equals(command)) {
                fd.setValue("${resource.mydocument}");
                fd.setOwner(pCtx.getSession().getUser());
                fd.setReadOnly(false);
                fd.setFileLocation("users/" + 
                                   pCtx.getSession().getUser().getId());
                fd.changeModel();
            }

            if ("friendDirectory".equals(command)) {
                fd.setValue("${resource.mydocument}");

                String friend = event.getParameter("friend");
                SessionUser user = 
                    pCtx.getSession().getSecurityManager().findUser(pCtx, friend);
                fd.setOwner(user);
                fd.setReadOnly(true);
                fd.setFileLocation("users/" + friend);
                fd.changeModel();
            }

            if ("deleteDir".equals(command) && (fd.getFile() != null)) {
                if (fd.getFile() == fd.getRootNode().getUserObject()) {
                    return;
                }

                fd.getFile().delete(pCtx);
                fd.changeModel();
            }
        }
    }

    public void onDelete(WebContext pCtx, UIEvent event) {
        UIComponentImpl comp = (UIComponentImpl)event.getSource();
        FileDirectory fd = 
            (FileDirectory)comp.getPage().getDescendantByClientId("fileListTree");
        try {
            int seq = Integer.parseInt(event.getParameter("seq"));
            FileNode file = (FileNode)fd.getFileList().get(seq);
            file.delete(pCtx);
            fd.getFileList().remove(seq);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
